package org.jxht.service.impl;

import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import org.jxht.dto.AdminDto;
import org.jxht.dto.StudentDto;
import org.jxht.dto.TeacherDto;
import org.jxht.service.AdminServie;
import org.jxht.service.LoginService;
import org.jxht.service.StudentService;
import org.jxht.service.TeacherService;
import org.jxht.util.ConstantUtils;
import org.jxht.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by huang on 2017/3/19 0019.
 */
@Service
public class LoginServiceImpl implements LoginService{

    @Resource
    private StudentService studentService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private AdminServie adminServie;

    /**
     * 进行登录验证
     * @param userId
     * @param userPwd
     * @param status
     * @return
     */
    public String LoginVerification(int userId, String userPwd, String status) {

        if(ConstantUtils.STUDENT.equals(status)){
            StudentDto dto = new StudentDto(userId);
            StudentDto resultDto = studentService.queryStudentServiceSingle(dto);
            if(null != resultDto) {
                if (userPwd.equals(Encryption(resultDto.getStuPwd())))
                    return "SUCCESS";
                else
                    return "FAIL";
            }
            else
                return "FAIL";
        }
        else if(ConstantUtils.TEACHER.equals(status)){
            TeacherDto dto = new TeacherDto(userId);
            TeacherDto resultDto = teacherService.queryTeacherServiceSingle(dto);
            if(null != resultDto) {
                if (userPwd.equals(Encryption(resultDto.getTeaPwd())))
                    return "SUCCESS";
                else
                    return "FAIL";
            }
            else
                return "FAIL";
        }
        else {
            AdminDto dto = new AdminDto(userId);
            AdminDto resultDto = adminServie.queryAdminServiceSingle(dto);
            if(null != resultDto) {
                if (userPwd.equals(Encryption(resultDto.getAdmPwd())))
                    return "SUCCESS";
                else
                    return "FAIL";
            }
            else
                return "FAIL";
        }
    }

    /**
     * 进行加密
     * @param strEncryption 需加密的字符串
     * @return
     */
    public String Encryption(String strEncryption){
        return MD5Util.MD5Encryption(MD5Util.convertEncryption(strEncryption));
    }
}
